package cn.com.bsoft.wePortal.partner.support
import cn.com.bsoft.kotlinEx.lowerUnderScore
import cn.com.bsoft.wePortal.partner.entity.Partner
import cn.com.bsoft.wePortal.partner.model.PartnerProperties
import cn.com.bsoft.wePortal.partner.service.PartnerService
import cn.com.bsoft.webEx.webApplicationContext
import javax.servlet.http.HttpServletRequest


fun HttpServletRequest.findValue(key:String):String?{
    var value = getHeader(key)
    if (value == null)
        value = getParameter(key.lowerUnderScore)
    return value
}

val HttpServletRequest.partnerCode:String? get() {
    val properties = webApplicationContext.getBean(PartnerProperties::class.java)

    return findValue(properties.keyPartnerCode)
}

val HttpServletRequest.partner:Partner? get() {
    val service = webApplicationContext.getBean(PartnerService::class.java)
    val code = partnerCode
    return if (code != null)
        service.query(code)
    else
        null
}

val HttpServletRequest.partnerSign:String? get() {
    val properties = webApplicationContext.getBean(PartnerProperties::class.java)

    return findValue(properties.keyPartnerSign)
}

val HttpServletRequest.partnerTimeStamp:String? get() {
    val properties = webApplicationContext.getBean(PartnerProperties::class.java)

    return findValue(properties.keyPartnerTimeStamp)
}